package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.flow.enums.FlowStateEnum;
import com.softer.wxzj.common.flow.enums.FlowTypeEnum;
import com.softer.wxzj.common.flow.util.FlowAddAuditUtil;
import com.softer.wxzj.common.flow.util.FlowAuditUtil;
import com.softer.wxzj.common.flow.util.Result;
import com.softer.wxzj.common.util.OperUtil;
import com.softer.wxzj.entity.OrgRegionAudit;
import com.softer.wxzj.mapper.OrgBuildAuditMapper;
import com.softer.wxzj.mapper.OrgRegionAuditMapper;
import com.softer.wxzj.service.OrgBuildAuditService;
import com.softer.wxzj.service.OrgRegionAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2020/5/28
 */
@Service
public class OrgRegionAuditServiceImpl implements OrgRegionAuditService {
    @Autowired
    private OrgRegionAuditMapper orgRegionAuditMapper;
    @Autowired
    private OperUtil zoneCodeUtil;
    @Autowired
    private FlowAuditUtil flowUtil;
    @Autowired
    private Result result;
    @Autowired
    private FlowAddAuditUtil flowAddAuditUtil;
    @Autowired
    private OrgBuildAuditService orgBuildAuditService;
    @Autowired
    private OrgBuildAuditMapper orgBuildAuditMapper;

    @Override
    public PageInfo<OrgRegionAudit> getRegionAuditList(Page page, OrgRegionAudit orgRegionAudit) {
        //赋值区域,重要
        orgRegionAudit.setZoneCode(zoneCodeUtil.getZoneCode());
        //参数转换
        //setFuzzyParam(baseOrganization);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<OrgRegionAudit> list = orgRegionAuditMapper.getRegionAuditList(orgRegionAudit);
        PageInfo<OrgRegionAudit> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 区域管理审核
     *
     * @param page
     * @param orgRegionAudit
     * @return
     */
    @Override
    public PageInfo<OrgRegionAudit> selectAuditByOrgRegion(Page page, OrgRegionAudit orgRegionAudit) {
        //查询业务   新增审核
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.REGION_BINDING.getType());
        List<OrgRegionAudit> list = new ArrayList<>();
        if (null != map) {
            result.selectParm(map, orgRegionAudit);
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = orgRegionAuditMapper.selectAuditByOrgRegion(orgRegionAudit);
        }
        PageInfo<OrgRegionAudit> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult submitAndAudit(String id, String opinion, String mark) {
        int i = NumberEnum.ZERO.getNum();
        String auditState = flowAddAuditUtil.audit(id, opinion, mark,null);
        if (null != auditState) {
            i = NumberEnum.ONE.getNum();
            //不在审核中则更新业务表
            if (!FlowStateEnum.IN_AUDIT.getState().equals(auditState)) {
                OrgRegionAudit orgRegionAudit = orgRegionAuditMapper.selectAllBuild(id);
                result.result(auditState, orgRegionAudit);
                i = orgRegionAuditMapper.updateFlowState(orgRegionAudit);
                if (FlowStateEnum.PASS.getState().equals(auditState)) {
                    orgBuildAuditService.changeRegionAndBuild(orgRegionAudit, orgRegionAudit.getBuildId(), id, orgRegionAudit.getCompanyId());
                } else {
                    orgBuildAuditMapper.updateAuditState(orgRegionAudit.getBuildId(), orgRegionAudit.getOperNo(), id, auditState);
                }
            }
        }
        return CommonResult.ResponseResult(i);
    }
}
